﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BloodApp.TCPCommunication
{
    public static class ProcessPacketData
    {
        private static MemoryStream _dataStream = new MemoryStream();
        private static ParserState _currentState = ParserState.SearchingHeader;
        private static ushort _packetLength = 0;
        private static byte[] _lengthBuffer = new byte[2];
        private static int _lengthIndex = 0;
        private static MemoryStream outStream = new MemoryStream();
        public static bool ParseNextByte(byte nextByte, out byte[] outBytes)
        {
            outStream.WriteByte(nextByte);
            //Console.WriteLine($"Received: {nextByte:X2}, State: {_currentState}");
            switch (_currentState)
            {
                case ParserState.SearchingHeader:
                    if (nextByte == 0xFF)
                    {
                        //Console.WriteLine("Found header.");
                        _lengthIndex = 0; // 重置长度缓冲区索引
                        _currentState = ParserState.ReadingLengthMsb; // 读取长度高位字节
                    }
                    break;
                case ParserState.ReadingLengthMsb:
                    //Console.WriteLine($"Length MSB: {nextByte}");
                    _lengthBuffer[_lengthIndex++] = nextByte;
                    if (_lengthIndex == 1) // 读取完毕，转向低位字节
                    {
                        _currentState = ParserState.ReadingLengthLsb;
                    }
                    break;
                case ParserState.ReadingLengthLsb:
                    //Console.WriteLine($"Length LSB: {nextByte}");
                    _lengthBuffer[_lengthIndex++] = nextByte;
                    _packetLength = (ushort)((_lengthBuffer[0] << 8) | _lengthBuffer[1]); // 组合成大端序长度
                    _dataStream = new MemoryStream();
                    _currentState = ParserState.ReadingData;
                    break;
                case ParserState.ReadingData:
                    //Console.WriteLine($"Data[{_dataStream.Length}]: {nextByte}");
                    _dataStream.WriteByte(nextByte);
                    if (_dataStream.Length == _packetLength)
                    {
                        _currentState = ParserState.CheckingFooter;
                    }
                    break;
                case ParserState.CheckingFooter:
                    if (nextByte == 0xEE)
                    {
                        outBytes = outStream.ToArray();
                        //ProcessPacket(outStream.ToArray());
                        outStream.Position = 0;
                        outStream.SetLength(0);
                        _currentState = ParserState.SearchingHeader; // 准备寻找新包头
                        return true; // 成功处理了一个包
                    }
                    else
                    {
                        //Console.WriteLine("Invalid footer, searching for new header.");
                        _currentState = ParserState.SearchingHeader; // 遇到非预期字节，重置状态
                    }
                    break;
            }
            outBytes = outStream.ToArray();
            return false; // 默认返回，直到一个包被完全解析
        }

        static void ProcessPacket(byte[] packetData)
        {
            Console.Write($"解析接收数据---->");
            foreach (var b in packetData)
            {
                Console.Write($"{b:X2} ");
            }
            Console.WriteLine($"------->{DateTime.Now.ToString("yyyyMMdd HH:mm:ss fff")}");
        }
    }

    public enum ParserState
    {
        SearchingHeader,
        ReadingLengthMsb,//高位字节
        ReadingLengthLsb,
        ReadingData,
        CheckingFooter
    }
}
